/**
 * 格式化gantt数据
 * @param {*} data
 * @param {*} currentWorkOrder
 * @returns
 */
export const formatterGanttData = (data, currentWorkOrder) => {
  let apsWorkOrderPlanVOList = [];
  if (data.resourceData && Object.keys(data.resourceData).length > 0) {
    for (const key in data.resourceData) {
      let sectionName = key.split('::')[0];
      let sectionId = key.split('::')[1];
      apsWorkOrderPlanVOList.push({
        id: sectionId,
        user: sectionName,
        text: sectionName,
        type: 'project',
        progress: 1,
        parent: 0,
        open: true,
        openLineDuration: 0,
        clearLineDuration: 0,
        uph: 0
      });
      if (data.resourceData[key]) {
        for (const cKey in data.resourceData[key]) {
          let lineName = cKey.split('::')[0];
          let lineId = cKey.split('::')[1];
          apsWorkOrderPlanVOList.push({
            id: lineId,
            user: sectionName,
            text: lineName,
            render: 'split',
            type: 'project',
            parent: sectionId,
            progress: 1,
            open: true,
            openLineDuration: 0,
            clearLineDuration: 0,
            uph: 0
          });
          if (data.resourceData[key] && data.resourceData[key][cKey] && data.resourceData[key][cKey].length > 0) {
            data.resourceData[key][cKey].map((r) => {
              if (r && r.apsWorkOrderPlanDetailVOList && r.apsWorkOrderPlanDetailVOList.length > 0) {
                r.apsWorkOrderPlanDetailVOList.map((rr, rIndex) => {
                  if (Number(rr.planQuantity)) {
                    apsWorkOrderPlanVOList.push({
                      id: rr.id,
                      user: sectionName,
                      // calendarDate: rr.calendarDate, // 年-月-日
                      // day: new Date(year, month - 1, date).getDay(), // 星期
                      text: r.workOrderNo,
                      status: rr.showStatus,
                      // isWorkTime: rr.dayWorkTime > 0,
                      planQuantity: rr.planQuantity,
                      // progress1 绿色
                      // rr的planEndDate<当前工单的planEndDate ：进度为1
                      // 反之进度 = rr的开始时间planStartDate=>当前工单的planEndDate持续时间/总时长
                      progress1:
                        currentWorkOrder.orderNo == r.workOrderNo
                          ? handleTransDate(currentWorkOrder.planEndDate, rr.planEndDate) < 0
                            ? 1
                            : (handleTransDate(rr.planStartDate, currentWorkOrder.planEndDate) / rr.numOfAllBetween).toFixed(5)
                          : 1,
                      // progress2 红色
                      progress2:
                        currentWorkOrder.orderNo == r.workOrderNo
                          ? handleTransDate(currentWorkOrder.planEndDate, rr.planEndDate) < 0
                            ? 0
                            : (1 - (handleTransDate(rr.planStartDate, currentWorkOrder.planEndDate) / rr.numOfAllBetween).toFixed(5)).toFixed(5)
                          : 0,
                      // progress3 橙色
                      // progress3: 0,
                      actualQuantity: rr.actualQuantity,
                      actualEndDate: rr.actualEndDate,
                      start_date: rr.showPlanStartDate, // 展示绘图用的时间
                      planStartDate: rr.planStartDate, // 真实开始时间
                      duration: rr.numOfAllBetween,
                      end_date: rr.planEndDate,
                      parent: lineId,
                      isCurrentOrder: currentWorkOrder.orderNo == r.workOrderNo,
                      progress: 1,
                      partNo: r.workOrderVO ? r.workOrderVO.partNo : '',
                      partName: r.workOrderVO ? r.workOrderVO.partName : ''
                    });
                  }
                });
              }
            });
          }
        }
      }
    }
  }
  return apsWorkOrderPlanVOList;
};

export const formatterReadOnlyGanttData = (data, currentWorkOrder) => {
  let apsWorkOrderPlanVOList = [];
  if (data.resourceData && Object.keys(data.resourceData).length > 0) {
    for (const key in data.resourceData) {
      let sectionName = key.split('::')[0];
      let sectionId = key.split('::')[1];
      apsWorkOrderPlanVOList.push({
        id: sectionId,
        user: sectionName,
        text: sectionName,
        type: 'project',
        progress: 1,
        parent: 0,
        open: true,
        openLineDuration: 0,
        clearLineDuration: 0,
        uph: 0
      });
      if (data.resourceData[key]) {
        for (const cKey in data.resourceData[key]) {
          let lineName = cKey.split('::')[0];
          let lineId = cKey.split('::')[1];
          apsWorkOrderPlanVOList.push({
            id: lineId,
            user: sectionName,
            text: lineName,
            render: 'split',
            type: 'project',
            parent: sectionId,
            progress: 1,
            open: true,
            openLineDuration: 0,
            clearLineDuration: 0,
            uph: 0
          });
          if (data.resourceData[key] && data.resourceData[key][cKey] && data.resourceData[key][cKey].length > 0) {
            for (const dKey in data.resourceData[key][cKey]) {
              data.resourceData[key][cKey].map((r) => {
                if (r && r.apsWorkOrderPlanDetailVOList && r.apsWorkOrderPlanDetailVOList.length > 0) {
                  r.apsWorkOrderPlanDetailVOList.map((rr, rIndex) => {
                    if (Number(rr.planQuantity)) {
                      apsWorkOrderPlanVOList.push({
                        id: rr.id,
                        user: sectionName,
                        text: r.workOrderNo,
                        status: rr.showStatus,
                        planQuantity: rr.planQuantity,
                        // progress1 绿色
                        // rr的planEndDate<当前工单的planEndDate ：进度为1
                        // 反之进度 = rr的开始时间planStartDate=>当前工单的planEndDate持续时间/总时长
                        progress1:
                          currentWorkOrder.orderNo == r.workOrderNo
                            ? handleTransDate(currentWorkOrder.planEndDate, rr.planEndDate) < 0
                              ? 1
                              : (handleTransDate(rr.planStartDate, currentWorkOrder.planEndDate) / rr.numOfAllBetween).toFixed(5)
                            : 1,
                        // progress2 红色
                        progress2:
                          currentWorkOrder.orderNo == r.workOrderNo
                            ? handleTransDate(currentWorkOrder.planEndDate, rr.planEndDate) < 0
                              ? 0
                              : (1 - (handleTransDate(rr.planStartDate, currentWorkOrder.planEndDate) / rr.numOfAllBetween).toFixed(5)).toFixed(5)
                            : 0,
                        // progress3 橙色
                        // progress3: 0,
                        actualQuantity: rr.actualQuantity,
                        actualEndDate: rr.actualEndDate,
                        start_date: rr.showPlanStartDate, // 展示绘图用的时间
                        planStartDate: rr.planStartDate, // 真实开始时间
                        duration: rr.numOfAllBetween,
                        end_date: rr.planEndDate,
                        parent: lineId,
                        isCurrentOrder: currentWorkOrder.orderNo == r.workOrderNo,
                        progress: 1,
                        partNo: r.workOrderVO ? r.workOrderVO.partNo : '',
                        partName: r.workOrderVO ? r.workOrderVO.partName : ''
                      });
                    }
                  });
                }
              });
            }
          }
        }
      }
    }
  }
  return apsWorkOrderPlanVOList;
};
/**
 * 排程异常提示状态描述
 * @param {*} data
 * @returns
 */
export const handleDispatchStatus = (data) => {
  let content = '';
  switch (data.resultStatus) {
    case 1:
      content = '修改的日计划数量大于该产线每日理论产出，请确认是否调整。';
      break;
    case 2:
      content = '计划开始时间至当前修改的结束时间期间，与其他工单生产时间有重合，请确认是否调整。';
      break;
    case 3:
      content = '当前产线排程计划结束时间超出工单计划结束时间，请确认是否继续。若继续，则导致工单延期。';
      break;
    case 4:
      content = '当天为非生产日，该排程将占用非生产日时间，请确认是否继续。若继续，请到[生产日历]中将该日设为生产日/特定日。';
      break;
    case 5:
      content = '当天为非生产日，该排程将占用非生产日时间，请确认是否继续。若继续，请到[生产日历]中将该日设为生产日/特定日。';
      break;
    case 6:
      content = '修改计划结束时间后，产线计划作业时间少于理论时间，请确认是否调整。';
      break;
    case 7:
      content = '当天为非生产日，该排程将占用非生产日时间，请确认是否继续。若继续，请到[生产日历]中将该日设为生产日/特定日。';
      break;
    case 8:
      content = '当前排程的计划开始至结束时间期间，与其他工单生产时间有重合，您可修改计划数量/其它时段，请确认是否继续。';
      break;
    default:
      break;
  }
  return content;
};

/**
 * 是否修改排程数据
 * @param {*} oldData
 * @param {*} newData
 * @returns
 */
export const isModifyDispatch = (oldData, newData) => {
  let modifyStatus = false;
  if (oldData === null) {
    return false;
  }
  if (oldData.length != newData.length) {
    modifyStatus = true;
  } else {
    for (let i = 0; i < oldData.length; i++) {
      // 工段/产线变化
      if (oldData[i].id != newData[i].id) {
        modifyStatus = true;
        break;
      }
      // 工单计划数量变化
      if (oldData[i].planQuantity != newData[i].planQuantity) {
        modifyStatus = true;
        break;
      }
      // 开始时间变化
      if (oldData[i].planStartDate != newData[i].planStartDate) {
        modifyStatus = true;
        break;
      }
      // 结束时间变化
      if (oldData[i].planEndDate != newData[i].planEndDate) {
        modifyStatus = true;
        break;
      }
      // 工段/产线变化
      if (oldData[i].sectionAndLine[0] != newData[i].sectionAndLine[0] || oldData[i].sectionAndLine[1] != newData[i].sectionAndLine[1]) {
        modifyStatus = true;
        break;
      }
      if (oldData[i].apsWorkOrderPlanDetailVOList && newData[i].apsWorkOrderPlanDetailVOList) {
        if (oldData[i].apsWorkOrderPlanDetailVOList.length != newData[i].apsWorkOrderPlanDetailVOList.length) {
          modifyStatus = true;
          break;
        }
        for (let j = 0; j < oldData[i].apsWorkOrderPlanDetailVOList.length; j++) {
          // 工段/产线变化
          if (oldData[i].apsWorkOrderPlanDetailVOList[j].id != newData[i].apsWorkOrderPlanDetailVOList[j].id) {
            modifyStatus = true;
            break;
          }
          // 日产量变化
          if (oldData[i].apsWorkOrderPlanDetailVOList[j].planQuantity != newData[i].apsWorkOrderPlanDetailVOList[j].planQuantity) {
            modifyStatus = true;
            break;
          }
        }
      }
    }
  }
  return modifyStatus;
};

/**
 * 比较时间大小算差值
 * @param {*} start
 * @param {*} end
 * @returns
 */
export const handleTransDate = (start, end) => {
  let startTime = new Date(start); //开始的时间
  let endTime = new Date(end); //结束的时间
  let gapTime = Math.floor((endTime - startTime) / (60 * 1000));
  return gapTime;
};
